package com.sky.service.impl;

import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public BusinessDataVO businessData(LocalDateTime beginTime, LocalDateTime endTime) {
        //1、查询某个时间段的营业额
        Map map = new HashMap();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        BusinessDataVO businessDataVO = new BusinessDataVO();
        Double turnover = orderMapper.sumTurnoverByMap(map);
        businessDataVO.setTurnover(turnover==null?0.00:turnover);
        //2、查询有效订单
        map.put("status", 5);
        Integer validOrderCount = orderMapper.countByMap(map);
        businessDataVO.setValidOrderCount(validOrderCount);
        Integer count = orderMapper.countByStatus(null);
        //计算订单完成率
        Double orderCompletionRate = count == null ? 0.00 : validOrderCount.doubleValue() / count;
        businessDataVO.setOrderCompletionRate(orderCompletionRate);

        //3、平均客单价
        Double unitPrice = (validOrderCount==null||turnover==null)?0.0:turnover / validOrderCount;
        businessDataVO.setUnitPrice(unitPrice);

        //4、新增用户 今日注册的用户
        Integer newUserCount = userMapper.countByMap(map);
        businessDataVO.setNewUsers(newUserCount);


        return businessDataVO;
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDate localDate = LocalDate.now();
        LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
        Map map = new HashMap();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("status", 2);
        Integer waitingOrders = orderMapper.countByMap(map);
        map.put("status", 3);
        Integer deliveredOrders = orderMapper.countByMap(map);
        map.put("status", 5);
        Integer completedOrders = orderMapper.countByMap(map);
        map.put("status", 6);
        Integer cancelledOrders = orderMapper.countByMap(map);
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);

        return OrderOverViewVO
                .builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        Map map = new HashMap();
        //停售的
        map.put("status", 0);
        Integer disableDishCount = dishMapper.countByMap(map);
        //起售的
        map.put("status", 1);
        Integer enableDishCount = dishMapper.countByMap(map);


        return DishOverViewVO.builder().sold(enableDishCount).discontinued(disableDishCount).build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Map map = new HashMap();
        //停售的
        map.put("status", 0);
        Integer disableDishCount = setmealMapper.countByMap(map);
        //起售的
        map.put("status", 1);
        Integer enableDishCount = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder().sold(enableDishCount).discontinued(disableDishCount).build();
    }
}
